#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2022/5/10 18:24
@File: data.py
"""
import os

from BladeFerm.Lib.Common import logger
from BladeFerm.utils.basic_opration import is_dir_exist
from BladeFerm.utils.dataoperation import YamlOperation, YamlData


def load_data(filename):
    '''
    读取测试数据，参数化
    :param filename: 数据文件路径
    :return:
    '''
    fp, fn = os.path.split(filename)
    if os.path.exists(filename):
        YOP = YamlOperation(filename)
        raw = YOP.readYaml()
        logger.info(f'{fn} test raw data : {raw}')
        input_data = raw.get('input_data', [None])
        ui_expect = raw.get('ui_expect', [None])
        input_data = [None] if len(input_data) == 0 else input_data
    else:
        logger.error(f'测试数据文件不存在，检查路径 {filename}')
        input_data, ui_expect = [None], [None]
    num = len(input_data) - len(ui_expect)
    if num > 0:
        for _ in range(num):
            ui_expect.append(None)
        logger.warning(f'期望值相比输入值少 {num} 个！')
    params = list(zip(input_data, ui_expect))
    logger.info(f'{fn} test params : {params}')
    return params


def yaml_to_excel(yaml_path, excel_path='./Blade_data'):
    res_info = dict()
    try:
        new_path = os.path.join(yaml_path, excel_path)
        is_dir_exist(new_path)
        file_lst = os.listdir(yaml_path)
        for file in file_lst:
            name, ext = os.path.splitext(file)
            if ext == '.yaml' or ext == '.yml':
                yaml_file = os.path.join(yaml_path, file)
                excel_path = os.path.join(new_path, name + '.xls')
                YD = YamlData(yaml_file)
                first_data = YD.input_data_to_dict(YD.input_data)
                result_list = YD.split_input_data(first_data)
                YD.data_to_excel(data_list=result_list, excel_path=excel_path)
            else:
                logger.warning(f'{file} 不支持数据转换成blade要求格式!')
        res_info.update({'return_code': 1, 'msg': '转换成功', 'new_path': new_path})
    except Exception as e:
        res_info.update({'return_code': 0, 'msg': '转换失败', 'error': str(e), 'new_path': ''})
    finally:
        return res_info



if __name__ == '__main__':
    yaml_path = '/金融反洗钱项目/web应用/基础公共脚本/data'
    yaml_to_excel(yaml_path, 'sdf')